<?php

//命名空间
namespace home\controller;
//引入公共控制器
use \core\Controller;

class IndexController extends Controller{
    //默认方法
    public function index(){
        //接收数据检索信息
        $cond=array();
        if (isset($_GET['c_id']) && $_GET['c_id'] !=0) $cond['c_id'] = (int)$_GET['c_id'];
        if (isset($_REQUEST['a_title']) && !empty(trim($_REQUEST['a_title']))) $cond['a_title']=trim($_REQUEST['a_title']);
        //获取分页数据
        $page=$_GET['page'] ?? 1;
        global $config;
        $pagecount=$config['home']['articles_pagecount'] ?? 5 ;

        //获取分页类信息
        $c = new \home\model\CategoryModey();
        $categories = $c->getAllCategories();
        //保存到session
        @$_SESSION['categories']=$categories;
        //获取所有博文的信息、
        $a=new \home\model\ArticleModel();
        $articles = $a->getAllrticles($cond);
        //获取满足条件的记录数
        $counts= $a->getCounts();
        //显示模板
        //获取分类下对应的博文数量
        $cat_counts= $a ->getCountsByCategory();
        //        获取分页字符
        $pagstr=\vendor\Page::clickPage(URL . 'index.php',$counts,$pagecount,$page,$cond);
        //获取最新目录
        $news=$a->getNewsInfo();
        $this->assign('pagstr',$pagstr);
        $this->assign('cond',$cond);
        $this->assign('news',$news);
        $this->assign('cat_counts',$cat_counts);
        $this->assign('articles',$articles);
       	$this->display('blogShowList.html');
    }
    //显示博文明细
    public function detail(){
        //接收数据
        $id=(int)$_GET['id'];
        //获取文章
        $a=new \home\model\ArticleModel();
        $article=$a->getById($id);

        //开启session
        @session_start();
        if (!isset($_SESSION['categories'])){
            $c = new \home\model\CategoryModey();
            $categories = $c->getAllCategories();
            //保存到session
            @$_SESSION['categories']=$categories;
        }
        //获取全部评论
        $c= new \home\model\CommentModel();
        $comments=$c->getAllComment($id);

        //分配显示
        $this->assign('comments',$comments);
        $this->assign('article',$article);
        $this->display('blogShow.html');
    }
}


